/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */

var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var imgTool = require("helper/picture");
var createTopicUrl = circleUrl.url.topic.create;

/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var headerLeft = ui(addheader+".headerLeft");//左上角-返回
headerLeft.on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "发布一个话题"});
header.refreshData();

//右上角-发布（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "发布";
headerRight_lableNext.visible = true;

//5秒防止多次点击
headerRight.on("touch", "", 5000, function() {
	submitAddTopic();
})
/****************处理页面-头部-end*****************/
var layout_main = ui("do_LinearLayout_main");

var addtopicRemark = layout_main.add("topicRemark", "source://view/reuse/form/mutiTextarea.ui",0,0);
var topicRemark = ui("topicRemark");
hashdata.addData({
    "textAreaHint":'发布精彩内容，可获得更多评论和点赞哦'
});
topicRemark.bindData(hashdata);
topicRemark.refreshData();

/****************点击-添加图片-begin*****************/
var imgGridView = ui("imgGrid");
var imgMax = 9;  //最多显示图片
var imgsData = []; //保存图片数组
/*添加图片*/
var do_ALayout_camera = ui("do_ALayout_camera");
do_ALayout_camera.on("touch", function(data, e) {
	if (imgMax==0) {
		nf.toast("最多只能上传 9 张图片");
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});
/*删除图片*/
imgGridView.on("touch",function(data, e){
	imgMax=imgMax+1;
	imgsData.splice(data,1);
	imgGridView.items = imgsData;
});
function selectImgBack(imgList){
	imgMax=imgMax-imgList.length;
	imgsData = imgsData.concat(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.items = imgsData;
}
/****************点击-添加图片-end*****************/

//andriod返回键
page.on("back", function() {
    app.closePage();
});

var circleId = null;
page.on("loaded", function(){
	circleId = page.getData().circleId;
});

//提交-按钮
function submitAddTopic(){
	var imgCount = imgsData.length;//图片数量
	var topicRemarkText = ui(addtopicRemark + '.textArea')
	var contentText = topicRemarkText.text;
	if(imgCount==0 && contentText.length==0){
		nf.toast("内容和图片不能都为空!");
		return;
	}
	var imgFiles  = [];
	for (var i = 0; i < imgCount; i++) {
		imgFiles.push(imgsData[i].imgPath);
	}
	
	if(imgCount>0){
		http.form(imgFiles,"circle/topic",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			var topic = {
					"circleId":circleId,
					"content":contentText,
					"img":string4imgs
					};
			httpSilence.post(createTopicUrl,topic,function(data){
				nf.toast("发布成功");
				app.closePage({"topic_add":true});
			});
		});	
	}else{
		var topic = {
				"circleId":circleId,
				"content":topicRemarkText.text,
				"img":""
				};
		http.post(createTopicUrl,topic,function(data){
			nf.toast("发布成功");
			app.closePage({"topic_add":true});
		});
	}
}